<!-- 函数式组件: render, createElement, JSX 用法示例 -->
<template>
  <div class="container">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column
        v-for="(column, index) of columns"
        :key="index"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        :align="column.align"
      >
        <template slot-scope="scope">
          <column-dom v-if="column.render" :column="column" :row="scope.row" :render="column.render" :index="index" />
          <span v-else-if="column.formatter">{{ column.formatter(scope.row, column) }}</span>
          <span v-else>{{ scope.row[column.prop] }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>

export default {
  name: 'TableRender',
  // 函数式组件: https://cn.vuejs.org/v2/guide/render-function.html#%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6
  components: {
    'column-dom': {
      functional: true,
      props: {
        row: Object,
        render: Function,
        index: Number,
        column: {
          type: Object,
          required: true,
        },
      },
      render: (createElement, context) => {
        const data = {
          row: context.props.row,
          index: context.props.index,
          column: context.props.column,
        };
        return context.props.render(createElement, data);
      },
    },
  },
  data() {
    return {
      tableData: [{
        id: 1,
        date: '2016-05-02',
        address: '上海市普陀区金沙江路 1518 弄',
        ucardType: 1,
      }, {
        id: 2,
        date: '2016-05-04',
        address: '上海市普陀区金沙江路 1517 弄',
        ucardType: 2,
      }, {
        id: 3,
        date: '2016-05-01',
        address: '上海市普陀区金沙江路 1519 弄',
        ucardType: 4,
      }, {
        id: 4,
        date: '2016-05-03',
        address: '上海市普陀区金沙江路 1516 弄',
        ucardType: 5,
      }],
      columns: [
        {
          prop: 'ucardType',
          label: '证件类型',
          formatter: (row, column) => {
            let ucardName = '';
            console.log('column :>> ', column);
            switch (row.ucardType) {
              case 1:
                ucardName = '居民身份证';
                break;
              case 2:
                ucardName = '护照';
                break;
              case 3:
                ucardName = '军官证';
                break;
              case 4:
                ucardName = '台胞证';
                break;
              case 5:
                ucardName = '回乡证(港澳居民来往内地通行证)';
                break;
              default:
            }
            return ucardName;
          },
        },
        {
          label: '操作',
          align: 'center',
          width: 120,
          render: (createElement, data) => createElement(
            'div',
            {
              class: 'cell',
            },
            [
              createElement(
                'el-button',
                {
                  attrs: {
                    type: 'text',
                  },
                  on: {
                    click: () => { this.edit(data.row); },
                  },
                },
                '编辑',
              ),
              createElement(
                'el-button',
                {
                  attrs: {
                    type: 'text',
                  },
                  on: {
                    click: () => { this.del(data.row); },
                  },
                },
                '取消',
              ),
            ],
          ),
        }, // createElement 参数, https://cn.vuejs.org/v2/guide/render-function.html#createElement-%E5%8F%82%E6%95%B0
        {
          label: '备注',
          align: 'center',
          width: 120,
          render: (h, data) => (
            <div class="cell">
              <el-button onclick={ () => this.comment(data.row) } type="text">
                备注
              </el-button>
            </div>
          ), // JSX 写法, https://cn.vuejs.org/v2/guide/render-function.html#JSX
        },
      ],
    };
  },
  methods: {
    // 点击编辑
    edit(row) {
      console.log('点击编辑 :>> ', row);
    },
    // 点击删除
    del(row) {
      console.log('点击删除 :>> ', row);
    },
    // 点击备注
    comment(row) {
      console.log('点击备注 :>> ', row);
    },
  },
};
</script>

<style lang="scss" scoped>

</style>
